/*
 * @Date: 2023-03-06 22:35:31
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-05-12 14:05:28
 * @Description: 公共接口
 */
import Qs from 'Qs'
import axios from '@/utils/axios.request'
import config from '@/config'
import { getLocalItem, setLocalItem, isNotEmpty } from '@/utils/baseTool'
/**
 * 获取获取菜单路由
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const findMenuTree = () => {
  return axios.post({
    url: '/api/user/menuTree',
    withCredentials: true,
    data: Qs.stringify({})
  })
}

/**
 * 获取用户查询条件配置
 * @param viewNo 视图编号
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const findSearchFormConfig = (viewNo: string) => {
  return axios.get({
    url: '/api/layout/findSearchFormConfig',
    withCredentials: true,
    params: {
      viewNo
    }
  })
}

/**
 * 获取用户表格配置
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const api_getTableConfig = (viewNo: string) => {
  return axios.request({
    url: '/api/layout/findTableConfig',
    method: 'post',
    withCredentials: true,
    params: {
      viewNo
    }
  })
}

/**
 * 获取用户表格配置
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const api_getTableList = (url: string, params: any) => {
  return axios.request({
    url,
    method: 'post',
    withCredentials: true,
    data: Qs.stringify(params, { allowDots: true })
  })
}

/**
 * 获取用户查询条件配置（所有字段，用于配置页面）
 * @param {string} viewNo 视图编号
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const getAllSearchFormConfig = (viewNo: string) => {
  return axios.request({
    url: '/api/layout/findAllSearchFormConfig',
    method: 'post',
    withCredentials: true,
    params: {
      viewNo
    }
  })
}

/**
 * 获取用户表格配置
 * @param {string} viewNo 视图编号
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const getTableConfig = (viewNo: string) => {
  return axios.request({
    url: '/api/layout/findTableConfig',
    method: 'post',
    withCredentials: true,
    params: {
      viewNo
    }
  })
}

/**
 * 用户配置还原
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const userFormConfigDelete = (viewId) => {
  return axios.request({
    url: '/api/layout/userFormConfigDelete',
    method: 'post',
    withCredentials: true,
    params: {
      viewId
    }
  })
}

/**
 * 用户配置保存
 * @returns {ClientRequest | * | AxiosPromise<any> | ClientHttp2Stream | void}
 */
export const userFormConfigSave = (params) => {
  return axios.request({
    url: '/api/layout/userFormConfigSave',
    method: 'post',
    withCredentials: true,
    data: Qs.stringify(params)
  })
}

interface IfindDictList {
  type: string
  parentId?: string
  parentValue?: string
}
export const apiFindDictList = ({ type, parentId, parentValue }: IfindDictList) => {
  let key = `dict_${type}_${parentId}_${parentValue}`
  let resdata: any = undefined
  //排除过滤的字典数据，每次都从远处获取
  // if (config.filterDictList.indexOf(type) === -1) {
  //   //从缓存里取，没取到时再进行本地js字典数据获取
  //   resdata = getLocalItem(key);
  //   if (resdata == undefined) {
  //     let list = getDictListByParentId(type, parentId, parentValue);
  //     if (list != undefined && list.length > 0) {
  //       resdata = {
  //         status: 200,
  //         data: {
  //           errmsg: "字典信息获取成功！",
  //           errorCode: 0,
  //           errorMsg: "字典信息获取成功！",
  //           isSuccess: true,
  //           data: list
  //         }
  //       }
  //     }
  //   }
  // }

  // let resdata = baseStoreUtils.getLocalItem(key);
  //有数据时直接返回，缓存或本地数据没有数据时，远程获取数据
  if (isNotEmpty(resdata)) {
    return new Promise((resolve, reject) => {
      resolve(resdata)
    })
  } else {
    let params = {
      type: type,
      parentId: parentId,
      parentValue: parentValue
    }

    // type=monitor_department时，获取监察机构
    const url =
      type != 'monitor_department'
        ? '/api/common/dict/findDictList'
        : '/api/espapp/eqmBcwBasic/findMonitorDictListByEpBcw'
    return axios
      .request({
        url,
        data: Qs.stringify(params),
        method: 'post',
        withCredentials: true
      })
      .then((res) => {
        setLocalItem(key, res)
        return res
      })
  }
}

/**
 * 根据字典类型，id 获取字典信息
 * @param { String } type
 * @param { String } value
 */
export const apiFindDictItem = ({ type, value }: { type: string; value: string }) => {
  // let key = `dict_item_${type}_${value}`

  // let resdata = baseStoreUtils.getLocalItem(key);
  // if(isNotEmpty(resdata)){
  //   return new Promise((resolve, reject) => {
  //     resolve(resdata)
  //   })
  // }else {
  const data = new URLSearchParams()
  data.append('type', type)
  data.append('value', value)
  return axios
    .request({
      url: '/api/common/dict/findDictItem',
      data,
      method: 'post',
      withCredentials: true
    })
    .then((res) => {
      // baseStoreUtils.saveLocalItem(key, res )
      return res
    })
  // }
}
